/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package hxg.portmgr.core.device;

import hxg.portmgr.core.oper.snmp.DslamSnmpOper;
import hxg.portmgr.core.oper.snmp.SnmpOper;
import hxg.portmgr.core.oper.snmp.BasSnmpOper;
import hxg.portmgr.core.oper.snmp.RouterSnmpOper;
import hxg.portmgr.core.oper.snmp.SwitchSnmpOper;
import com.google.gson.Gson;
import hxg.portmgr.core.oper.snmp.ServerSnmpoper;
import java.io.*;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author hxg
 */
public enum DeviceType {

    ROUTER {

        @Override
        public RouterSnmpOper getDefaultSnmpOper(String ip, String readCommunity, String writeCommunity) {
            return new RouterSnmpOper(ip, readCommunity, writeCommunity);
        }
    }, BAS {

        @Override
        public BasSnmpOper getDefaultSnmpOper(String ip, String readCommunity, String writeCommunity) {
            return new BasSnmpOper(ip, readCommunity, writeCommunity);
        }
    }, SWITCH {

        @Override
        public SwitchSnmpOper getDefaultSnmpOper(String ip, String readCommunity, String writeCommunity) {
            return new SwitchSnmpOper(ip, readCommunity, writeCommunity);
        }

        @Override
        public String[] getEssentialParam() {
            return new String[]{"vlan", "toBasIntf"};
        }
    }, DSLAM {

        @Override
        public DslamSnmpOper getDefaultSnmpOper(String ip, String readCommunity, String writeCommunity) {
            return new DslamSnmpOper(ip, readCommunity, writeCommunity);
        }
    }, SERVER {

        @Override
        public SnmpOper getDefaultSnmpOper(String ip, String readCommunity, String writeCommunity) {
            return new ServerSnmpoper(ip, readCommunity, writeCommunity);
        }
    }, OTHER {

        @Override
        public SnmpOper getDefaultSnmpOper(String ip, String readCommunity, String writeCommunity) {
            return new SnmpOper(ip, readCommunity, writeCommunity);
        }
    };
    Set<String> attrParam = new LinkedHashSet<String>();

    public Collection<String> getParams() {
        if (attrParam == null || attrParam.isEmpty()) {
            loadAttr();
        }
        Set<String> params = new LinkedHashSet<String>();
        params.addAll(Arrays.asList(getEssentialParam()));
        params.addAll(attrParam);
        return Collections.unmodifiableSet(params);
    }

    public void addParam(String param) {
        attrParam.add(param.toLowerCase().trim());
        saveAttr();
    }

    public void removeParam(String param) {
        attrParam.add(param.toLowerCase().trim());
        saveAttr();
    }

    private void loadAttr() {
        Gson gson = new Gson();
        File f = new File("\\data\\type\\" + name() + ".attr");
        if (!f.exists()) {
            f.mkdirs();
        }
        try {
            Reader r = new BufferedReader(new FileReader("\\data\\type\\" + name() + ".attr"));
            LinkedHashSet<String> strset = gson.fromJson(r, LinkedHashSet.class);
            attrParam.addAll(strset);
            r.close();
        } catch (IOException ex) {
            Logger.getLogger(DeviceType.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    private void saveAttr() {
        Gson gson = new Gson();
        File f = new File("\\data\\type\\" + name() + ".attr");
        if (!f.exists()) {
            f.mkdirs();
        }
        try {
            Writer r = new BufferedWriter(new FileWriter("\\data\\type\\" + name() + ".attr", false));
            gson.toJson(attrParam, LinkedHashSet.class, r);
            r.close();
        } catch (IOException ex) {
            Logger.getLogger(DeviceType.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public abstract <T extends SnmpOper> T getDefaultSnmpOper(String ip, String readCommunity, String writeCommunity);

    public String[] getEssentialParam() {
        return new String[0];
    }
 
    
}
